﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using Tools;

namespace Tools
{
    internal class NavButton : MButton, INavControl<NavButton>
    {
        string UserControlName { set; get; }
        public XmlNode NavXmlNode { set; get; }

        public List<NavButton> NavButtons
        {
            get
            {
                List<NavButton> list = new List<NavButton>();

                foreach (XmlNode node in NavXmlNode.ChildNodes)
                {
                    NavButton nav = GetNavButtonFromXmlNode(node);
                    if (nav != null)
                        list.Add(nav);
                }

                return list;
            }
        }

        private NavButton GetNavButtonFromXmlNode(XmlNode node)
        {
            XmlAttribute nameAttr = node.Attributes["name"];
            XmlAttribute ucAttr = node.Attributes["uc"];

            if (nameAttr == null || string.IsNullOrWhiteSpace(nameAttr.Value))
                return null;

            NavButton nav = new NavButton();
            nav.Text = nameAttr.Value.Trim();
            nav.NavXmlNode = node;
            if (ucAttr != null && !string.IsNullOrWhiteSpace(ucAttr.Value))
            {
                nav.UserControlName = ucAttr.Value.Trim();
            }

            return nav;
        }

        public NavButton()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();

            this.Size = new System.Drawing.Size(150, 150);
            this.Margin = new Padding(20);
            this.Click += new EventHandler(NavButton_Click);
            this.BackColor = System.Drawing.Color.Transparent;
            this.Image = Tools.Properties.Resources.bgImage;
            this.FlatAppearance.BorderSize = 0;
            this.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
            this.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ForeColor = System.Drawing.Color.White;
            Cursor = System.Windows.Forms.Cursors.Hand;
            this.ResumeLayout(false);
        }

        void NavButton_Click(object sender, EventArgs e)
        {
            Control parent = this.Parent;

            if (NavButtons.Count > 0)
            {
                parent.Controls.Clear();
                foreach (NavButton nav in NavButtons)
                {
                    parent.Controls.Add(nav);
                }
            }
            else if (!string.IsNullOrWhiteSpace(UserControlName))
            {
                if (parent != null)
                {
                    Type t = Type.GetType(UserControlName);
                    if (t == null)
                    {
                        this.Text = "Invalid UserControl";
                        return;
                    }

                    Control control = Activator.CreateInstance(t) as Control;
                    foreach (Control item in parent.Controls)
                    {
                        item.Hide();
                    }
                    parent.Controls.Add(control);
                }
            }
        }
    }
}
